package com.stgy.lease.common.result;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.Getter;

/**
 * ClassName:Result
 * Package:com.stgy.lease.common.result
 * Description:jackson使用result对象 转json字符串 需要使用getter方法
 *
 * @Author:helei
 * @Create:2024/8/28 - 14:02
 */
@Getter
@Schema(description = "统一响应类")
public class Result<T> {

    //浏览器解析响应状态码200时，才解析响应体内容
    @Schema(description = "业务状态码:0成功")
    private Integer code;
    private String message;
    private Boolean ok;
    private T data;

    private Result() {
    }

    //1、创建result对象的方法
    public static <T> Result<T> build(Integer code, String message, Boolean ok, T data) {
        Result<T> result = new Result<>();
        result.code = code;
        result.message = message;
        result.ok = ok;
        result.data = data;
        return result;
    }

    public static <T> Result<T> build(ResultCodeEnum resultCodeEnum, Boolean ok, T data) {
        return build(resultCodeEnum.getCode(), resultCodeEnum.getMessage(), ok, data);
    }

    //2、成功方法
    public static <T> Result<T> ok(T data) {
        return build(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage(), true, data);
    }

    public static <T> Result<T> ok() {
        return Result.ok(null);
    }

    /*
    SUCCESS(200, "成功"),
    FAIL(201, "失败"),
    PARAM_ERROR(202, "参数不正确"),
    SERVICE_ERROR(203, "服务异常"),
    DATA_ERROR(204, "数据异常"),
    ILLEGAL_REQUEST(205, "非法请求"),
    REPEAT_SUBMIT(206, "重复提交"),
    DELETE_ERROR(207, "请先删除子集"),

    ADMIN_ACCOUNT_EXIST_ERROR(301, "账号已存在"),
    ADMIN_CAPTCHA_CODE_ERROR(302, "验证码错误"),
    ADMIN_CAPTCHA_CODE_EXPIRED(303, "验证码已过期"),
    ADMIN_CAPTCHA_CODE_NOT_FOUND(304, "未输入验证码"),

    ADMIN_LOGIN_AUTH(305, "未登陆"),
    ADMIN_ACCOUNT_NOT_EXIST_ERROR(306, "账号不存在"),
    ADMIN_ACCOUNT_ERROR(307, "用户名或密码错误"),
    ADMIN_ACCOUNT_DISABLED_ERROR(308, "该用户已被禁用"),
    ADMIN_ACCESS_FORBIDDEN(309, "无访问权限"),

    APP_LOGIN_AUTH(501, "未登陆"),
    APP_LOGIN_PHONE_EMPTY(502, "手机号码为空"),
    APP_LOGIN_CODE_EMPTY(503, "验证码为空"),
    APP_SEND_SMS_TOO_OFTEN(504, "验证法发送过于频繁"),
    APP_LOGIN_CODE_EXPIRED(505, "验证码已过期"),
    APP_LOGIN_CODE_ERROR(506, "验证码错误"),
    APP_ACCOUNT_DISABLED_ERROR(507, "该用户已被禁用"),

    TOKEN_EXPIRED(601, "token过期"),
    TOKEN_INVALID(602, "token非法");
    */
    //3、失敗方法
    public static <T> Result<T> fail(Integer code, String message) {
        return build(code, message, false, null);
    }

    //使用枚举类对象创建result对象
    public static <T> Result<T> fail(ResultCodeEnum resultCodeEnum) {
        return build(resultCodeEnum.getCode(), resultCodeEnum.getMessage(), false, null);
    }

    public static <T> Result<T> fail() {
        return build(ResultCodeEnum.FAIL.getCode(), ResultCodeEnum.FAIL.getMessage(), false, null);
    }

    public static <T> Result<T> build(boolean b) {
        return b ? Result.ok() : Result.fail();
    }

    //4、修改属性值链式调用的方法
    public Result<T> code(Integer code) {
        this.code = code;
        return this;
    }

    public Result<T> code(String message) {
        this.message = message;
        return this;
    }

    public Result<T> code(Boolean ok) {
        this.ok = ok;
        return this;
    }

    public Result<T> code(T data) {
        this.data = data;
        return this;
    }


}
